package com.easy.generator.java;

/**
 * @author easy
 * @date 2025年07月05日 08:41
 */

import com.easy.generator.Easy;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDate;

public class FileGenerator {
    private static final String CLASS_NAME="FileUploadController";
    private static final String FILE_CONTROLLER_CONTENT =
                    "package "+ Easy.CONTROLLER_PACKAGE+";\n" +
                    "\n" +
                    "import "+ Easy.UTIL_PACKAGE+".Result;\n" +
                    "import org.springframework.util.StringUtils;\n" +
                    "import org.springframework.web.bind.annotation.PostMapping;\n" +
                    "import org.springframework.web.bind.annotation.RequestMapping;\n" +
                    "import org.springframework.web.bind.annotation.RequestParam;\n" +
                    "import org.springframework.web.bind.annotation.RestController;\n" +
                    "import org.springframework.web.multipart.MultipartFile;\n" +
                    "\n" +
                    "import java.nio.file.Files;\n" +
                    "import java.nio.file.Path;\n" +
                    "import java.nio.file.Paths;\n" +
                    "\n" +
                    "@RestController\n" +
                    "@RequestMapping(\"upload\")\n" +
                    "public class "+CLASS_NAME+" {\n" +
                    "    private static final String UPLOAD_DIR = \""+Easy.FILE_STATIC_PATH+"\";\n" +
                    "\n" +
                    "    // 单文件上传\n" +
                    "    @PostMapping\n" +
                    "    public Result<String> uploadFile(@RequestParam(\"file\") MultipartFile file) {\n" +
                    "        try {\n" +
                    "            String fileName = StringUtils.cleanPath(file.getOriginalFilename());\n" +
                    "            Path uploadPath = Paths.get(UPLOAD_DIR);\n" +
                    "            if (!Files.exists(uploadPath)) {\n" +
                    "                Files.createDirectories(uploadPath);\n" +
                    "            }\n" +
                    "            Path filePath = uploadPath.resolve(fileName);\n" +
                    "            file.transferTo(filePath);\n" +
                    "            return Result.success(200,\"文件上传成功\" ,"+"\""+Easy.FILE_URL_PREFIX+"/\""+"+fileName);\n" +
                    "        } catch (Exception e) {\n" +
                    "            return Result.success(500,\"上传失败:\" +e.getMessage(),null);\n" +
                    "        }\n" +
                    "    }\n" +
                    "}\n";

    private static void generateFileController() throws IOException{
        String content = String.format(FILE_CONTROLLER_CONTENT, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(Easy.CONTROLLER_PACKAGE)+"/"+CLASS_NAME+".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }

    private static final String LOCAL_FILE_WEB_CONFIG=
            "package "+ Easy.CONFIG_PACKAGE+";\n" +
            "\n" +
            "import org.springframework.context.annotation.Configuration;\n" +
            "import org.springframework.http.CacheControl;\n" +
            "import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;\n" +
            "import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;\n" +
            "\n" +
            "import java.util.concurrent.TimeUnit;\n" +
            "\n" +
            "@Configuration\n" +
            "public class LocalFileWebConfig implements WebMvcConfigurer {\n" +
            "    @Override\n" +
            "    public void addResourceHandlers(ResourceHandlerRegistry registry) {\n" +
            "        // 添加自定义路径（本地磁盘路径）\n" +
            "        registry.addResourceHandler(\""+ Easy.FILE_URL_PREFIX+"/**\")\n" +
            "                .addResourceLocations(\"file:"+ Easy.FILE_STATIC_PATH+"\")\n" +
            "                .setCacheControl(CacheControl.maxAge(1, TimeUnit.HOURS));\n" +
            "    }\n" +
            "}";

    private static final String CONFIG_CLASS_NAME="LocalFileWebConfig";
    private static void generateLocalFileWebConfig() throws IOException{
        String content = String.format(LOCAL_FILE_WEB_CONFIG, LocalDate.now());
        File outputFile = new File(Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(Easy.CONFIG_PACKAGE)+"/"+CONFIG_CLASS_NAME+".java");
        try (FileWriter writer = new FileWriter(outputFile)) {
            writer.write(content);
        }
    }
    private static void init(){
        String controllerPackagePath= Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(Easy.CONTROLLER_PACKAGE)+"/";
        File controllerPackage=new File(controllerPackagePath);
        if (!controllerPackage.exists()) {
            controllerPackage.mkdirs();
        }
        String configPackagePath= Easy.PROJECT_ABSOLUTE_URL+ Easy.SRC_DIR+ Easy.convertPackagePath(Easy.CONFIG_PACKAGE)+"/";
        File configPackage=new File(configPackagePath);
        if (!configPackage.exists()) {
            configPackage.mkdirs();
        }

    }
    public static void generate() throws IOException {
        init();
        generateFileController();
        generateLocalFileWebConfig();
    }

}
